package com.skyline.wo.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.common.bean.Page;
import com.skyline.common.cache.annotation.Cache;
import com.skyline.common.cache.annotation.CacheCategoryType;
import com.skyline.common.cache.annotation.CacheDelete;
import com.skyline.common.cache.annotation.Fk;
import com.skyline.common.cache.annotation.Param;
import com.skyline.wo.dao.ShortMessageDao;
import com.skyline.wo.mapper.ShortMessageMapper;
import com.skyline.wo.model.ShortMessage;

@Repository("shortMessageDao")
public class ShortMessageDaoImpl extends BaseDaoImpl implements ShortMessageDao {

	@Value("${ShortMessageDao.insertShortMessage}")
	private String insertShortMessageSql;

	@Value("${ShortMessageDao.queryShortMessage}")
	private String queryShortMessageSql;

	@Value("${ShortMessageDao.queryShortMessageByOwnerId}")
	private String queryShortMessageByOwnerIdSql;

	@Value("${ShortMessageDao.deleteShortMessage}")
	private String deleteShortMessageSql;

	@Override
	@CacheDelete(type = CacheCategoryType.SHORTMESSAGE)
	public long insertShortMessage(@Fk long ownerId, String ownerNickname, String ownerPortrait, String text, String comment,
			long sourceOwnerId, String sourceOwnerNickname) {
		return insertWithIdReturn(insertShortMessageSql, ownerId, ownerNickname, ownerPortrait, text, comment, sourceOwnerId,
				sourceOwnerNickname);
	}

	@Override
	@Cache(keyPattern = "shortMess-:id", type = CacheCategoryType.SHORTMESSAGE)
	public ShortMessage queryShortMessage(@Param("id") long id) {
		List<ShortMessage> messages = jdbcTemplate.query(queryShortMessageSql, ShortMessageMapper.getMapper(), id);
		if (messages != null && messages.size() > 0) {
			return messages.get(0);
		} else {
			return null;
		}
	}

	@Override
	@Cache(keyPattern = "shortMess-l-:uid", type = CacheCategoryType.SHORTMESSAGE)
	public List<ShortMessage> queryShortMessageByOwnerId(@Param("uid") long ownerId, Page page) {
		return getPaginationResult(queryShortMessageByOwnerIdSql, page, ShortMessageMapper.getMapper(), ownerId);
	}

	@Override
	@CacheDelete(type = CacheCategoryType.SHORTMESSAGE)
	public void deleteShortMessage(long id, @Fk long actionerId) {
		jdbcTemplate.update(deleteShortMessageSql, id, actionerId);
	}
}
